package com.suray.wcs.service.plc.thread;

import com.suray.basic.wcs.plc.pojo.oper.single.FoldingOper;
import com.suray.basic.wcs.utils.LoggerType;
import com.suray.basic.wcs.utils.LoggerUtil;
import com.suray.basic.wcs.utils.ModuleType;
import com.suray.basic.wcs.utils.WcsFuncUtil;
import com.suray.basic.wcs.utils.thread.BaseThread;
import com.suray.wcs.service.plc.util.FoldingManualType;
import com.suray.wcs.service.pojo.rgv.move.speed.NormalActionStatus;
import com.suray.wcs.service.pojo.rgv.move.speed.RgvMoveBySpeedThread;

import java.util.HashMap;
import java.util.Map;

/**
 * 手动行驶线程
 */
public class FoldingManualAction extends BaseThread {

    private static Map<FoldingOper, FoldingManualAction> foldingOperManualMap = new HashMap<>();

    //按速度行驶状态
    private NormalActionStatus actionStatus;

    private FoldingManualType manualType;

    private FoldingManualType manualTypeTemp;

    private FoldingOper foldingOper;

    private FoldingManualAction(FoldingOper foldingOper) {
        super(foldingOper.getDeviceName() + " 手动操控", 1000, RgvMoveBySpeedThread.class);

        this.foldingOper = foldingOper;
        actionStatus = NormalActionStatus.STANDY;
    }

    public static synchronized FoldingManualAction getInstance(FoldingOper foldingOper) {
        FoldingManualAction foldingManualAction = foldingOperManualMap.get(foldingOper);
        if (foldingManualAction == null) {
            foldingManualAction = new FoldingManualAction(foldingOper);
            foldingManualAction.start();
            foldingOperManualMap.put(foldingOper, foldingManualAction);
        }

        return foldingManualAction;
    }

    @Override
    protected void process() {
        try {
            while (actionStatus == NormalActionStatus.ACTION) {
                LoggerUtil.info(ModuleType.PLC, LoggerType.FOLDINNG_MANUAL_ACTION, foldingOper.getDeviceName(),
                        actionStatus, manualType.toString() + "中", this.getClass());
                switch (manualType) {
                    case UP:
                        foldingOper.manualUp();
                        break;
                    case DOWN:
                        foldingOper.manualDown();
                        break;
                    case PUSH:
                        foldingOper.manualPush();
                        break;
                    case RETRACT:
                        foldingOper.manualRetract();
                        break;
                    default:
                        throw new RuntimeException("手动操作类型错误");
                }
                WcsFuncUtil.delay(100);
            }
            if (actionStatus == NormalActionStatus.STOP_ACTION) {
                actionStatus = NormalActionStatus.STANDY;
                LoggerUtil.info(ModuleType.PLC, LoggerType.FOLDINNG_MANUAL_ACTION, foldingOper.getDeviceName(),
                        actionStatus, manualType + "停止成功", this.getClass());
            }
        } catch (Exception e) {
            LoggerUtil.info(ModuleType.PLC, LoggerType.ERROR, foldingOper.getDeviceName(), actionStatus, e.getMessage(),
                    this.getClass());
            actionStatus = NormalActionStatus.STANDY;
        }
    }

    /**
     * 按速度停止
     */
    public void stopAction() {
        synchronized (actionStatus) {
            if (actionStatus == NormalActionStatus.ACTION) {
                actionStatus = NormalActionStatus.STOP_ACTION;
            }
        }
        LoggerUtil.info(ModuleType.PLC, LoggerType.FOLDINNG_MANUAL_ACTION, foldingOper.getDeviceName(), actionStatus,
                manualType.toString() + " 停止", this.getClass());
    }

    /**
     * 按速度行驶
     */
    public void startAction(FoldingManualType manualType) {
        synchronized (actionStatus) {
            this.manualType = manualType;
            actionStatus = NormalActionStatus.ACTION;
        }
        LoggerUtil.info(ModuleType.PLC, LoggerType.FOLDINNG_MANUAL_ACTION, foldingOper.getDeviceName(), actionStatus,
                manualType.toString() + " 开始", this.getClass());
    }
}
